<template>
	<view class="page-container">
		<!-- 卡片容器 -->
		<view class="card">
			<view class="title">售货机系统</view>

			<view class="form">
				<!-- 用户名 -->
				<view class="input-item">
					<input v-model="userName" type="text" placeholder="请输入用户名" class="input" />
				</view>

				<!-- 密码 -->
				<view class="input-item password-item">
					<input v-model="password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码"
						class="input" @focus="showPasswordToggle = true" @blur="handlePasswordBlur" />
					<text class="password-toggle" v-show="showPasswordToggle" @mousedown="handleToggleMouseDown"
						@click="togglePassword">
						{{ showPassword ? '👁️' : '🔒' }}
					</text>
				</view>

				<!-- 错误提示 -->
				<text class="error" v-if="error">{{ error }}</text>

				<!-- 登录按钮 -->
				<button class="button login-btn" :loading="loading" @click="onLoginButtonClicked">
					登录
				</button>

				<!-- 注册跳转 -->
				<button class="button register-btn" @click="onRegisterButtonClicked">
					新用户？注册
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	let tag = "login";

	export default {
		data() {
			return {
				userName: "",
				password: "",
				error: "",
				loading: false,
				showPassword: false,
				showPasswordToggle: false,
				blurTimer: null
			}
		},
		methods: {
			onRegisterButtonClicked() {
				this.$log.info(tag, "onRegisterButtonClicked");
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			onLoginButtonClicked() {
				this.$log.info(tag, "onLoginButtonClicked");
				if (this.userName != "" && this.password != "") {
					this.$log.info(tag, "not null check pass")
				} else {
					this.error = '用户名和密码不可以为空';
					this.$log.error(tag, "用户名和密码不可以为空")
					return
				}

				this.loading = true;
				uni.request({
					url: `${this.constant.BASE_URL}/api/customer/login`,
					method: 'POST',
					data: {
						"password": this.password,
						"username": this.userName
					},
					success: (res) => {
						this.loading = false;
						this.$log.info(tag, "success");
						this.$log.info(tag, JSON.stringify(res.data));
						if (res.data.code == 200) {
							this.loginManager.setToken(res.data.token)
							uni.showToast({
								icon: 'success',
								title: "登录成功"
							})
							uni.navigateTo({
								url: "/pages/product-list/product-list"
							})
						} else {
							this.error = res.data.msg || '登录失败';
							uni.showToast({
								icon: 'none',
								title: res.data.msg || '登录失败'
							})
						}
					},
					fail: (err) => {
						this.loading = false;
						this.$log.error(tag, err.errMsg);
						this.error = '网络错误，请稍后重试';
						uni.showToast({
							icon: 'none',
							title: '网络错误，请稍后重试'
						})
					}
				})
			},
			togglePassword() {
				this.showPassword = !this.showPassword;
			},
			handleToggleMouseDown(e) {
				e.preventDefault();
			},
			handlePasswordBlur() {
				this.blurTimer = setTimeout(() => {
					this.showPasswordToggle = false;
				}, 150);
			}
		},
		beforeDestroy() {
			if (this.blurTimer) {
				clearTimeout(this.blurTimer);
			}
		}
	}
</script>

<style scoped>
	* {
		box-sizing: border-box;
		margin: 0;
		padding: 0;
	}

	.page-container {
		padding: 60rpx 20rpx;
		min-height: 100vh;
		background-color: #f0f2f5;
		display: flex;
		justify-content: center;
		align-items: flex-start;
	}

	.card {
		width: 100%;
		max-width: 500rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 40rpx 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
	}

	.title {
		text-align: center;
		font-size: 38rpx;
		font-weight: 600;
		color: #1677ff;
		margin-bottom: 40rpx;
	}

	.form {
		width: 100%;
	}

	.input-item {
		margin-bottom: 40rpx;
		position: relative;
	}

	.password-item {
		display: flex;
		align-items: center;
	}

	.input {
		width: 100%;
		height: 80rpx;
		font-size: 28rpx;
		color: #333;
		padding: 18rpx 20rpx;
		padding-right: 80rpx;
		border: 1px solid #e0e0e0;
		border-radius: 10rpx;
		background-color: #fff;
		transition: border-color 0.3s ease;
	}

	.input:focus {
		border-color: #1677ff;
		outline: none;
		box-shadow: 0 0 0 4rpx rgba(22, 119, 255, 0.1);
	}

	.password-toggle {
		position: absolute;
		right: 0;
		color: #1677ff;
		font-size: 36rpx;
		padding: 20rpx;
		cursor: pointer;
		user-select: none;
	}

	.error {
		display: block;
		color: #ff4d4f;
		font-size: 26rpx;
		margin: -20rpx 0 40rpx;
		height: 30rpx;
	}

	.button {
		width: 100%;
		font-size: 32rpx;
		font-weight: 500;
		border-radius: 10rpx;
		margin-bottom: 15rpx;
		transition: all 0.2s ease;
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border: none;
	}

	.login-btn {
		background-color: #1677ff;
		color: #fff;
		box-shadow: 0 2rpx 8rpx rgba(22, 119, 255, 0.2);
	}

	.login-btn:active {
		background-color: #0066cc;
		transform: scale(0.98);
		box-shadow: 0 1rpx 4rpx rgba(22, 119, 255, 0.15);
	}

	.register-btn {
		background-color: #fff;
		color: #1677ff;
		border: 1px solid #1677ff;
		box-shadow: none;
	}

	.register-btn:active {
		background-color: rgba(22, 119, 255, 0.05);
	}
</style>